<?php

namespace app\v1\model;

use think\Model;
use think\Db;

/**
 * Class Sign
 * @package app\v1\model
 * 模块说明：这个模块是用来处理签到的数据信息
 */
class Sign extends Base
{
    //获取签到列表
    public function getSignList(){

    }

    //获取签到总信息
    public function getStudentSignInfo(){

    }

    //获取托管的签到列表
    public function getStudentSignList(){
        $userInfo=getUserInfo();
        $user=model('v1/User');
        $childInfo=$user->getCommonInfo($userInfo['childId'],$userInfo['orgId'],$userInfo['schoolId']);
        $signCount=$this->getSignCount(0);
        $leaveCount=$this->getSignCount(1);
        $absentCount=$this->getEnrollCount()-$signCount-$leaveCount;
        $signInfo=array('signCount'=>$signCount,'leaveCount'=>$leaveCount,'absentCount'=>$absentCount);

        $startTime=strtotime(date('Y-m'));
        $startDate=date('Y-m-d',$startTime);
        $endDate=date('Y-m-t');
        //$endTime=strtotime(date('Y-m-t'));
        $map['cs._orgid']=$userInfo['orgId'];
        $map['cs._shopid']=$userInfo['schoolId'];
       // $map['cs.leave']=0;
        $map['cs.effect']=1;
        $map['cs.active']=1;
        $map['cs._studentid']=$userInfo['childId'];
        $signList=Db::name('childcare_sign')->alias('cs')->join('childcare_enroll ce','cs.enrollid=ce.id')->where($map)
            ->where("date>='{$startDate}' and date<='{$endDate}'" )->field('cs.date as theDate,cs.transfertime,cs.entertime,cs.signofftime,ce.typeid,cs.leave')->limit($this->pageCount)->page($this->currentPage)->order('cs.id desc')->select();
       /* foreach($signList as $key=>$value){

        }*/
        $data=array('childInfo'=>$childInfo,'signInfo'=>$signInfo,'current_page'=>$this->currentPage,'signList'=>$signList);
        renderData(0,'ok',$data);
    }

    //算出学生报名的总数
    public function getEnrollCount(){
        $startTime=strtotime(date('Y-m'));
        $startDate=date('Y-m-d',$startTime);
        $today=date('d');
        $count=0;
        for($i=1;$i<=$today;$i++){
            $theDate=date('Y-m');
            if($i<10){
                $thisDate=$theDate.'-0'.$i;
            }else{
                $thisDate=$theDate.'-'.$i;
            }
            $res=$this->checkEnroll($thisDate);
            if($res){
                $count++;
            }
        }
        return $count;

    }

    //获取本月请假或者签到的条数$type 1:请假 0:正常签到
    public function getSignCount($type){
        $userInfo=getUserInfo();
        $map['leave']=$type;
        $map['_studentid']=$userInfo['childId'];
        $map['_orgid']=$userInfo['orgId'];
        $map['_shopid']=$userInfo['schoolId'];
        $startTime=strtotime(date('Y-m'));
        $startDate=date('Y-m-d',$startTime);
        $endDate=date('Y-m-t');
        $count=Db::name('Childcare_sign')->where($map)->where("date>='{$startDate}' and date<='{$endDate}'" )->distinct('date')->count();
        return $count;
    }
    //查询某天是否有报名(效率问题,一天取一次就行,放redis)
    public function checkEnroll($theDate){
        $userInfo=getUserInfo();
        $map['studentid']=$userInfo['childId'];
        $res=Db::name('Childcare_enroll')->where($map)->where("startdate<="."'".$theDate."'"." and enddate>="."'".$theDate."'")->limit(1)->count();
        if(!empty($res)){
            return true;
        }else{
            return false;
        }
    }




}
